<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/8
 * Time: 16:30
 */

namespace App\Http\Controllers\API;

use App\Http\Controllers\ApiController;
use Curl\Curl;
use Illuminate\Support\Facades\DB;
use Validator;
use Illuminate\Http\Request;

class ArticlesController extends ApiController
{
    public function getLists(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'page' => 'required|Integer|min:1',
            'perpage' => 'Integer|min:1'
        ]);

        if ($validator->fails()) {
            return $this->outWarning($validator->errors(), 400);
        }

        $page = $request->input('page');
        $perPage = $request->input('perpage', 10);

        $Article = DB::table('ArticleS')->orderBy('id', 'desc')->select('id', 'title', 'images')->paginate($perPage, ['*'], 'page', $page);
        return $this->outSuccessData($Article);
    }

    public function getDetail(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'id' => 'required|Integer|min:1'
        ]);

        if ($validator->fails()) {
            return $this->outWarning($validator->errors(), 400);
        }

        new \Swoole();


        $id = $request->input('id');
        $curl = new \Curl\Curl();
        $res = $curl->get("http://news-at.zhihu.com/api/2/news/" . $id);
        return $this->outSuccessData($curl->response);
    }

}